﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.GetImportedCardRequest
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using Microsoft.InfoCards.Diagnostics;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace Microsoft.InfoCards
{
  internal class GetImportedCardRequest : UIAgentRequest
  {
    private InfoCard m_card;
    private Recipient m_issuer;
    private string m_filename;
    private string m_policyLink;
    private InfoCardPolicy m_policy;
    private GetImportedCardRequest.ImportCardMatchStatus m_matchFlag;

    public GetImportedCardRequest(
      IntPtr rpcHandle,
      Stream inArgs,
      Stream outArgs,
      ClientUIRequest parent)
      : base(rpcHandle, inArgs, outArgs, parent)
    {
      this.m_matchFlag = GetImportedCardRequest.ImportCardMatchStatus.MatchNone;
    }

    protected override void OnInitializeAsSystem()
    {
      base.OnInitializeAsSystem();
      this.m_policy = this.GetPolicy();
    }

    protected override void OnMarshalInArgs()
    {
      this.m_filename = Utility.DeserializeString((BinaryReader) new InfoCardBinaryReader(this.InArgs, Encoding.Unicode));
    }

    protected override void OnProcess()
    {
      StoreConnection connection = StoreConnection.GetConnection();
      try
      {
        InfoCardTrace.Assert(!string.IsNullOrEmpty(this.m_filename), "No file name provided for import");
        InfoCardXmlSerializer cardXmlSerializer = new InfoCardXmlSerializer(connection);
        cardXmlSerializer.CheckSignature = true;
        cardXmlSerializer.Deserialize(this.m_filename);
        this.m_card = cardXmlSerializer.Card;
        this.m_card.IsImported = true;
        this.m_card.InstalledOn = DateTime.Now;
        X509Certificate2 issuer = cardXmlSerializer.Issuer;
        if (issuer == null || this.m_card == null || !this.m_card.IsComplete())
          throw InfoCardTrace.ThrowHelperError((Exception) new ImportException(SR.GetString("InvalidImportFile")));
        Recipient.RecipientCertParameters recipientParams;
        string recipientIdHash = Recipient.CertGetRecipientIdHash(issuer, cardXmlSerializer.AdditionalIssuerCerts, cardXmlSerializer.IsIssuerChainTrusted, out recipientParams);
        string organizationIdHash = Recipient.CertGetRecipientOrganizationIdHash(issuer, cardXmlSerializer.AdditionalIssuerCerts, cardXmlSerializer.IsIssuerChainTrusted);
        this.ParentRequest.CertCacheAdd(recipientIdHash, issuer);
        this.m_issuer = new Recipient(issuer, recipientIdHash, organizationIdHash, true, this.m_card.PrivacyPolicyVersion, recipientParams);
        this.m_card.IssuerName = !this.m_issuer.IsOrganizationVerified() ? this.m_issuer.RecipientParameters.m_cn : this.m_issuer.RecipientParameters.m_organization;
        this.m_policyLink = this.m_card.PrivacyPolicyLink;
        DataRow singleRow = connection.GetSingleRow(QueryDetails.FullRow, new List<QueryParameter>()
        {
          new QueryParameter("ix_objecttype", new object[1]
          {
            (object) 1
          }),
          new QueryParameter("ix_globalid", new object[1]
          {
            (object) GlobalId.DeriveFrom(this.m_card.Id.ToString())
          })
        }.ToArray());
        if (singleRow == null)
          return;
        using (MemoryStream memoryStream = new MemoryStream(singleRow.GetDataField()))
        {
          InfoCard card = new InfoCard((Stream) memoryStream);
          this.m_matchFlag = (int) card.Epoch != (int) this.m_card.Epoch ? (card.Epoch >= this.m_card.Epoch ? GetImportedCardRequest.ImportCardMatchStatus.MatchNewerVersion : GetImportedCardRequest.ImportCardMatchStatus.MatchOlderVersion) : GetImportedCardRequest.ImportCardMatchStatus.MatchSameVersion;
          this.m_card.CopyMetaData(card);
        }
      }
      finally
      {
        connection.Close();
      }
    }

    protected override void OnMarshalOutArgs()
    {
      Stream outArgs = this.OutArgs;
      if (this.m_card == null || this.m_issuer == null || !this.m_card.IsComplete())
        return;
      StoreConnection connection = StoreConnection.GetConnection();
      try
      {
        this.m_card.AgentSerialize(outArgs, this.ParentRequest is GetTokenRequest, this.m_policy, connection, new CultureInfo(this.ParentRequest.UserLanguage));
        BinaryWriter writer = new BinaryWriter(outArgs, Encoding.Unicode);
        this.m_issuer.Serialize(writer);
        Utility.SerializeString(writer, this.m_policyLink);
        writer.Write((byte) this.m_matchFlag);
      }
      finally
      {
        connection.Close();
      }
    }

    public enum ImportCardMatchStatus : byte
    {
      MatchNone,
      MatchSameVersion,
      MatchNewerVersion,
      MatchOlderVersion,
    }
  }
}
